package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceDedParamDAO;
import org.jsola.hr.dto.AttendanceDedParamAddDTO;
import org.jsola.hr.dto.AttendanceDedParamUpdateDTO;
import org.jsola.hr.entity.AttendanceDedParamDO;
import org.jsola.hr.query.AttendanceDedParamQuery;
import org.jsola.hr.service.IAttendanceDedParamService;
import org.jsola.hr.vo.AttendanceDedParamListVO;
import org.jsola.hr.vo.AttendanceDedParamVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendanceDedParamServiceImpl")
public class AttendanceDedParamServiceImpl implements IAttendanceDedParamService {

    @Autowired
    private IAttendanceDedParamDAO attendanceDedParamDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceDedParamVO save(AttendanceDedParamAddDTO attendanceDedParamAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceDedParamDO attendanceDedParamDO = attendanceDedParamAddDTO.to(AttendanceDedParamDO.class);
        // 保存
        attendanceDedParamDO = save(attendanceDedParamDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增考勤扣款设置参数", "saveAttendanceDedParam", attendanceDedParamDO.getId(), tokenUser);
        return attendanceDedParamDO.to(AttendanceDedParamVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceDedParamUpdateDTO attendanceDedParamUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceDedParamDO attendanceDedParamDO = attendanceDedParamUpdateDTO.to(AttendanceDedParamDO.class);
        HrLogKit.logForUpdate("修改考勤扣款设置参数", "updateAttendanceDedParam", attendanceDedParamDO.getId(), tokenUser);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceDedParamDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceDedParamIds) {
        HrLogKit.logForDelete("批量删除考勤扣款设置参数", "deleteAttendanceDedParamByIds"
                , Arrays.toString(attendanceDedParamIds), tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceDedParamIds);
    }


    @Override
    public AttendanceDedParamVO selectById(String attendanceDedParamId, String siteId) {
        AttendanceDedParamDO attendanceDedParamDO = selectDOById(attendanceDedParamId, siteId);
        if (attendanceDedParamDO == null) {
            return null;
        }
        return attendanceDedParamDO.to(AttendanceDedParamVO.class);
    }

    @Override
    public List<AttendanceDedParamListVO> select(AttendanceDedParamQuery attendanceDedParamQuery, String siteId) {
        List<AttendanceDedParamDO> attendanceDedParamDOList = selectDO(attendanceDedParamQuery, siteId);
        if (CollectionUtils.isEmpty(attendanceDedParamDOList)) {
            return attendanceDedParamDOList == null ? null : new ArrayList<>();
        }
        return attendanceDedParamDOList.stream()
                .map(attendanceDedParamDO -> attendanceDedParamDO.to(AttendanceDedParamListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceDedParamQuery attendanceDedParamQuery, String siteId) {
        Example example = buildExample(attendanceDedParamQuery, siteId);
        return attendanceDedParamDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceDedParamListVO> selectPage(AttendanceDedParamQuery attendanceDedParamQuery, String siteId) {
        Example example = buildExample(attendanceDedParamQuery, siteId);
        Page<AttendanceDedParamDO> page = attendanceDedParamDAO.selectPageByExample(example,
                attendanceDedParamQuery.getPageNo(),
                attendanceDedParamQuery.getPageSize());

        return page.to(AttendanceDedParamListVO.class);
    }

    @Override
    public AttendanceDedParamDO selectDOById(String attendanceDedParamId, String siteId) {
        return listById(attendanceDedParamId, siteId);
    }

    @Override
    public List<AttendanceDedParamDO> selectDO(AttendanceDedParamQuery attendanceDedParamQuery, String siteId) {
        Example example = buildExample(attendanceDedParamQuery, siteId);
        return attendanceDedParamDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param attendanceDedParamQuery 查询参数
     * @param siteId                  所属站点id
     * @return example
     */
    private Example buildExample(AttendanceDedParamQuery attendanceDedParamQuery, String siteId) {
        Example example = new Example(AttendanceDedParamDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendanceDedParamQuery.getItemId() != null) {
            example.and().andLike("itemId", "%" + attendanceDedParamQuery.getItemId() + "%");
        }
        if (attendanceDedParamQuery.getTimesOperator() != null) {
            example.and().andEqualTo("timesOperator", attendanceDedParamQuery.getTimesOperator());
        }
        if (attendanceDedParamQuery.getTimesValue() != null) {
            example.and().andEqualTo("timesValue", attendanceDedParamQuery.getTimesValue());
        }
        if (attendanceDedParamQuery.getDedType() != null) {
            example.and().andEqualTo("dedType", attendanceDedParamQuery.getDedType());
        }
        if (attendanceDedParamQuery.getDedAmount() != null) {
            example.and().andEqualTo("dedAmount", attendanceDedParamQuery.getDedAmount());
        }
        if (attendanceDedParamQuery.getAbsenteeismType() != null) {
            example.and().andEqualTo("absenteeismType", attendanceDedParamQuery.getAbsenteeismType());
        }
        if (attendanceDedParamQuery.getAbsenteeismValueStart() != null &&
                attendanceDedParamQuery.getAbsenteeismValueEnd() != null) {
            example.and().andBetween("absenteeismValue", attendanceDedParamQuery.getAbsenteeismValueStart(),
                    attendanceDedParamQuery.getAbsenteeismValueEnd());
        }
        if (attendanceDedParamQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendanceDedParamQuery.getCompanyId());
        }
        if (attendanceDedParamQuery.getGmtCreateStart() != null &&
                attendanceDedParamQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendanceDedParamQuery.getGmtCreateStart(),
                    attendanceDedParamQuery.getGmtCreateEnd());
        }
        if (attendanceDedParamQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendanceDedParamQuery.getCreateUserId() + "%");
        }
        if (attendanceDedParamQuery.getGmtModifiedStart() != null &&
                attendanceDedParamQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendanceDedParamQuery.getGmtModifiedStart(),
                    attendanceDedParamQuery.getGmtModifiedEnd());
        }
        if (attendanceDedParamQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendanceDedParamQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, attendanceDedParamQuery.getOrders());
        return example;
    }
}




